package com.paiai.lslease.action.apphouse;

import com.paiai.lslease.auto.model.AppSocket;
import com.paiai.lslease.base.AppResult;
import com.paiai.lslease.base.BaseAction;
import com.paiai.lslease.service.apphouse.AppHouseService;
import com.paiai.lslease.service.apphouse.AppSocketService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class AppSocketAction extends BaseAction {

    private static final Logger logger = LoggerFactory.getLogger(AppSocketAction.class);

    private AppSocketService appSocketService;
    private AppHouseService appHouseService;

    private String operateUserId;
    private String houseId;
    private String socketId;
    private String name;
    private String mac;
    private String wifiSsid;
    private String wifiPassword;

    public void setOperateUserId(String operateUserId) {
        this.operateUserId = operateUserId;
    }

    public void setHouseId(String houseId) {
        this.houseId = houseId;
    }

    public void setSocketId(String socketId) {
        this.socketId = socketId;
    }

    public void setAppSocketService(AppSocketService appSocketService) {
        this.appSocketService = appSocketService;
    }

    public void setAppHouseService(AppHouseService appHouseService) {
        this.appHouseService = appHouseService;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }

    public void setWifiSsid(String wifiSsid) {
        this.wifiSsid = wifiSsid;
    }

    public void setWifiPassword(String wifiPassword) {
        this.wifiPassword = wifiPassword;
    }

    /**
     * 添加一个插座
     */
    public void insertSocket() {
        try {
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作者ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作者ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作者未注册");
                return;
            }
            if (StringUtils.isEmpty(name)) {
                name = operateUserId + "的新插座";
            }
            if (StringUtils.isEmpty(mac)) {
                sendFailureMsg("插座的mac地址不能为空");
                return;
            }
            if (StringUtils.isEmpty(wifiSsid)) {
                wifiPassword = "";
            }
            AppSocket appSocketToInsert = new AppSocket();
            appSocketToInsert.setManageUserId(Integer.valueOf(operateUserId));
            appSocketToInsert.setName(name);
            appSocketToInsert.setMac(mac);
            appSocketToInsert.setWifiSsid(wifiSsid);
            appSocketToInsert.setWifiPassword(wifiPassword);
            appSocketToInsert.setItem1("1");
            appSocketToInsert.setInsertTime(new Date());
            appSocketToInsert.setUpdateTime(new Date());
            AppSocket appSocket = appSocketService.insertSocket(appSocketToInsert);
            if (appSocket == null) {
                logger.info("appSocket == null");
                sendFailureMsg("添加插座失败");
                return;
            }
            sendSuccesseMsg(appSocket, "添加插座成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("添加插座失败");
        }
    }

    /**
     * 删除一个插座
     */
    public void deleteSocket() {
        try {
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作者ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作者ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作者未注册");
                return;
            }
            if (StringUtils.isEmpty(socketId)) {
                sendFailureMsg("插座ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(socketId)) {
                sendFailureMsg("插座ID格式错误");
                return;
            }
            if (!appSocketService.checkSocketExist(Integer.parseInt(socketId))) {
                sendFailureMsg("插座不存在");
                return;
            }
            if (!appSocketService.deleteSocket(Integer.parseInt(socketId))) {
                logger.info("删除失败");
                sendFailureMsg("删除插座失败");
                return;
            }
            sendSuccesseMsg("删除插座成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("删除插座失败");
        }

    }

    /**
     * 修改插座信息
     */
    public void modifySocket() {
        try {
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作者ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作者ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作者未注册");
                return;
            }
            if (StringUtils.isEmpty(socketId)) {
                sendFailureMsg("插座ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(socketId)) {
                sendFailureMsg("插座ID格式错误");
                return;
            }
            AppSocket _appSocket = appSocketService.selectSocket(Integer.parseInt(socketId));
            if (_appSocket == null) {
                sendFailureMsg("插座不存在");
                return;
            }
            if (!operateUserId.equalsIgnoreCase(String.valueOf(_appSocket.getManageUserId()))) {
                sendFailureMsg("操作者不是插头的添加者，不能编辑插头信息");
                return;
            }
            if (StringUtils.isEmpty(wifiPassword)) {
                wifiSsid = "";
            }
            AppSocket appSocketToUpdate = new AppSocket();
            if (!StringUtils.isEmpty(houseId)) {
                if (!StringUtils.isNumeric(houseId)) {
                    sendFailureMsg("房源ID格式错误");
                    return;
                }
                if (!appHouseService.checkHouseExist(Integer.parseInt(houseId))) {
                    sendFailureMsg("房源不存在");
                    return;
                }
                appSocketToUpdate.setHouseId(Integer.valueOf(houseId));
            }
            if (StringUtils.isEmpty(houseId) && StringUtils.isEmpty(name) && StringUtils.isEmpty(wifiSsid) && StringUtils.isEmpty(wifiPassword)) {
                sendFailureMsg("请至少选择修改插头的一个属性");
                return;
            }
            appSocketToUpdate.setId(Integer.valueOf(socketId));
            appSocketToUpdate.setName(name);
            appSocketToUpdate.setWifiSsid(wifiSsid);
            appSocketToUpdate.setWifiPassword(wifiPassword);
            appSocketToUpdate.setUpdateTime(new Date());
            AppSocket appSocket = appSocketService.modifySocket(appSocketToUpdate);
            if (appSocket == null) {
                logger.info("更新插座失败");
                sendFailureMsg("更新插座失败");
                return;
            }
            sendSuccesseMsg(appSocket,"更新插座成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("更新插座失败");
        }
    }

    /**
     * 查询用户的所有插座
     */
    public void selectAllSocket() {
        try {
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作者ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作者ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作者未注册");
                return;
            }
            List<AppSocket> appSocketList = appSocketService.selectAllSocket(Integer.parseInt(operateUserId));
            sendSuccesseMsg(appSocketList, "查询房源下所有插座列表成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("查询房源下所有插座列表失败");
        }
    }

    /**
     * 根据插座ID查询单个插座信息
     */
    public void selectSingleSocketById() {
        try {
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作者ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作者ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作者未注册");
                return;
            }
            if (StringUtils.isEmpty(socketId)) {
                sendFailureMsg("插座ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(socketId)) {
                sendFailureMsg("插座ID格式错误");
                return;
            }
            AppSocket appSocket = appSocketService.selectSocket(Integer.parseInt(socketId));
            if (appSocket == null) {
                sendFailureMsg("插座不存在");
                return;
            }
            sendSuccesseMsg(appSocket, "根据插座ID查询单个插座信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("根据插座ID查询单个插座信息失败");
        }
    }
}
